<html>
<head><meta charset="utf-8"><title>waker docs · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html">waker docs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171787046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171787046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171787046">(Jul 26 2019 at 16:23)</a>:</h4>
<p>I've been reading the Future and Waker docs in std, and AFAICT we're not documenting the exact spec for executors with regards to Waker. Specifically, what the effect of <code>wake()</code> on Waker is during different points of the execution model. I think there are several points that would be worth explicitly noting in the lifecycle.</p>
<p>For example, it's not clear what happens if code like this is written:</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="w"> </span><span class="n">Future</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">MyType</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Output</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span>: <span class="nc">Pin</span><span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">Self</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">cx</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">Context</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Poll</span><span class="o">&lt;</span><span class="n">Self</span>::<span class="n">Output</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">cx</span><span class="p">.</span><span class="n">waker</span><span class="p">().</span><span class="n">wake_by_ref</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">Poll</span>::<span class="n">Pending</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Is this well behaved/reasonable code? Note that the wake call may happen on a separate thread, too, though that's not guaranteed. My expectation would be that this would work fine, and result in the future being polled indefinitely (i.e., essentially busy loop) but be guaranteed to <em>not</em> lead to recursion, that is no stack overflows.</p>



<a name="171787108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171787108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171787108">(Jul 26 2019 at 16:24)</a>:</h4>
<p>(maybe this is also not the right place to discuss this -- though it feels like a pretty foundational question)</p>



<a name="171798187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171798187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171798187">(Jul 26 2019 at 18:26)</a>:</h4>
<p>Yes, that is well-behaved reasonable code.</p>



<a name="171798199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171798199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171798199">(Jul 26 2019 at 18:26)</a>:</h4>
<p>It will repeatedly cause the future of <code>MyType</code> to be polled in a loop</p>



<a name="171816827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171816827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171816827">(Jul 26 2019 at 22:48)</a>:</h4>
<p>What are the expected effects of calling <code>wake</code> multiple times, prior to the poll method being called? Just one <code>poll</code> call? Multiple?</p>



<a name="171816883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171816883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171816883">(Jul 26 2019 at 22:49)</a>:</h4>
<p>Along similar lines, the documentation notes that the _latest_ Waker must be used to initiate waking; when does the "new" waker become so? After entry into <code>poll</code>? Part of why I ask is to try to understand <a href="https://docs.rs/futures-preview/0.3.0-alpha.17/futures/task/struct.AtomicWaker.html" target="_blank" title="https://docs.rs/futures-preview/0.3.0-alpha.17/futures/task/struct.AtomicWaker.html">AtomicWaker</a> -- and the use cases/need for it</p>



<a name="171816982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171816982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171816982">(Jul 26 2019 at 22:51)</a>:</h4>
<p>It seems like the <code>take</code> method in particular is a bit odd to me, since I can't really think of how it can be used in a reasonable way -- wouldn't the new owner of the returned Waker not be able to tell whether it's the latest Waker? (My understanding is that AtomicWaker is purely for ensuring that separate threads which intend to call Wake can keep track of the latest waker for a given future -- but maybe that's wrong?)</p>



<a name="171843517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/waker%20docs/near/171843517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nemo157 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/waker.20docs.html#171843517">(Jul 27 2019 at 12:29)</a>:</h4>
<p>Calling <code>wake</code> multiple times I believe must lead to _at least_ one call to <code>poll</code> (which called also be said to be exactly one call to poll and some extra spurious wakeups)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>